package com.wallet.my.controller;

import com.wallet.my.controller.model.ImportByMnemonicRequest;
import com.wallet.my.controller.model.TransRecordRequest;
import com.wallet.my.controller.model.TransRequest;
import com.wallet.my.dao.pojo.AccountDO;
import com.wallet.my.dao.pojo.TransRecordDO;
import com.wallet.my.service.EthService;
import com.wallet.my.service.ImportAccountService;
import com.wallet.my.service.ListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthTransaction;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.PublicKey;
import java.util.List;
import java.util.Map;

/**
 * 钱包的功能接口
 **/
@RestController
@RequestMapping("/Api")
public class EthController {
    @Autowired
    private EthService ethService;
    @Autowired
    private ImportAccountService importAccountService;
    @Autowired
    private ListenerService listenerService;

//获取连接信息
    @GetMapping("/getClient")
    public String getClientVersion(){
        return ethService.getClientVersion();
    }

////新建钱包，保存keystore文件，返回创建信息和钱包信息
//    @PostMapping("/newWallet")
//    public Map<String,Object> newWallet(@RequestBody String password,HttpSession session){
//        Map<String, Object> walletInfo = ethService.newWallet(password,session);
//        session.setAttribute("mnemonic",walletInfo.get("mnemonic"));
//        return walletInfo;
//    }

//    新建HD钱包，get方法，不生成keystore文件，返回钱包0号地址和公私钥信息
    @GetMapping("/newHDWallet")
    public Map<String,Object> newHDWallet(HttpSession session){
        return ethService.newHDWallet(session);
    }

//    新建HD账户，需要输入助记词，不生成keystore文件，返回钱包i号地址和公私钥信息M/44H/60H/0H/0/i
    @PostMapping("/newHDAccount")
    public Map<String,Object> newHDAccount(@RequestBody String mnemonic,HttpSession session){
        Map<String, Object> accountInfo = ethService.newHDAccount(mnemonic,session);
        return accountInfo;
}


////新建账户，这里比较难实现，用seed生成masterkeypair后，怎么用地址生成子私钥，并且怎么能遍历找回来
//    @PostMapping("/newAccount")
//    public Map<String,Object> newAccount(@RequestBody String mnemonic,HttpSession session){
//        Map<String, Object> accountInfo = ethService.newAccount(mnemonic,session);
//        return accountInfo;
//    }

//    查询登录用户名下的所有账户信息
    @GetMapping("/getAllAccountByName")
    public List<AccountDO> getAllAccountByName(HttpSession session){
        String username = (String)session.getAttribute("username");
        List<String> allAddress = ethService.getAllAddressByName(username);   //拿到用户名下所有地址
        listenerService.updataBalance(allAddress);          //更新当前用户库里所有地址余额信息
        List<AccountDO> allAccount = ethService.getAllAccountByName(username);   //返回所有账户信息
        return allAccount;
    }

//    根据地址查询余额
    @PostMapping("/getBalanceByAddress")
    public Map<String,Object> getBalanceByAddress(String address){
        return ethService.getBalance(address);
    }

//    转账
    @PostMapping("/sendTransaction")
    public Map<String,Object> sendTransaction(@RequestBody TransRequest transRequest){
        String fromAddr = transRequest.getFromAddr();
        String privateKey = transRequest.getPrivateKey();
        String toAddr = transRequest.getToAddr();
        BigDecimal money = new BigDecimal(transRequest.getMoney());
        BigInteger gasLimit = new BigInteger(transRequest.getGasLimit());
        String data = transRequest.getData();
        return ethService.transferETH(fromAddr, privateKey, toAddr, money, gasLimit, data);
    }

//    根据hash查询交易信息
    @PostMapping("/getTransInfoByHash")
    public EthTransaction getTransInfoByHash(String transHash){
        return ethService.getTransInfoByHash(transHash);
    }

    //    根据hash查询交易凭证
    @PostMapping("/getTransReceipt")
    public EthGetTransactionReceipt getTransReceipt(String transHash){
        return ethService.getTransReceipt(transHash);
    }

//    私钥导入账户
    @PostMapping("/importAccountByPrivateKey")
    public Map<String, Object> importAccountByPrivateKey(@RequestBody String privateKey,HttpSession session){
        return importAccountService.importAccountByPrivateKey(privateKey,session);
    }

//    keystore导入账户
    @PostMapping("/importAccountByKeystore")
    public Map<String, Object> importAccountByKeystore(@RequestBody String password, File keystore,HttpSession session) {
        return importAccountService.importAccountByKeystore(password, keystore,session);
    }

//    助记词导入指定分层地址的账户
    @PostMapping("/importAccountByMnemonic")
    public Map<String, Object> importAccountByMnemonic(@RequestBody ImportByMnemonicRequest importByMnemonicRequest, HttpSession session) {
        String mnemonic = importByMnemonicRequest.getMnemonic();
        String accountPath = importByMnemonicRequest.getAccountPath();
        return importAccountService.importAccountByMnemonic(mnemonic,accountPath,session);
    }

////    助记词导入一个根账户
//    @PostMapping("/importRootByMnemonic")
//    public Map<String, Object> importRootByMnemonic(String mnemonic) {
//    return importAccountService.importRootByMnemonic(mnemonic);
//    }

    //    更新库地址指定块区间的交易记录
    @PostMapping("/updataTransRecord")
    public String updataTransRecord(Long startBlockNum,Long endBlockNum){
        List<String> allAddress = ethService.getAllAddress();
        listenerService.addTransRecord(startBlockNum,endBlockNum,allAddress);
        return "更新后，请稍后查询交易记录";
    }

    //    开始监听当前最新区块的交易记录，开始后无法停止
    @GetMapping("/listenTransRecord")
    public String listenTransRecord(){
        List<String> allAddress = ethService.getAllAddress();
        listenerService.listenTransRecord(allAddress);
        return "开始监听当前最新区块的交易记录，开始后无法停止，可继续其他操作";
    }

    //    更新账户余额
    @GetMapping("/updataBalance")
    public String updataBalance(){
        List<String> allAddress = ethService.getAllAddress();
        listenerService.updataBalance(allAddress);
        return "余额更新中，可继续其他操作";
    }

    //    查询address相关的所有交易记录
    @PostMapping("/getTransRecordByAddress")
    public List<TransRecordDO> getTransRecordByAddress(@RequestBody TransRecordRequest transRecordRequest){
        List<TransRecordDO> transRecordByAddress = ethService.getTransRecordByAddress(transRecordRequest.getAccoutAddr());
        return transRecordByAddress;
    }

//    助记词和账户地址解锁账户，返回私钥
    @PostMapping("/unlockAccountByMnemonic")
    public Map<String, Object> unlockAccountByMnemonic(@RequestBody ImportByMnemonicRequest importByMnemonicRequest, HttpSession session) {
        String mnemonic = importByMnemonicRequest.getMnemonic();
        String accountPath = importByMnemonicRequest.getAccountPath();
        return importAccountService.unlockAccountByMnemonic(mnemonic,accountPath,session);
    }



}
